package yja.gzw.app.activity

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import com.bumptech.glide.Glide
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityQiyebangdingBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class QiyebangdingActivity :BaseActivity<Mainviewmodel, ActivityQiyebangdingBinding>(R.layout.activity_qiyebangding){

    var file: File?=null
    var tupiadizhi=""
    override fun initView() {
        binding.yingyezhizhaotu.setOnClickListener {
            openGallery()


        }
        binding.fanhui.setOnClickListener {
            finish()

        }
        binding.queding.setOnClickListener {
            val sfzhao=binding.sfzshurukuang.text.toString()
            val shoujihao=binding.sjhshurukuang.text.toString()
            val qieyname=binding.qiyename.text.toString()
            val youxiangdizhi=binding.youxiangdizhi.text.toString()
            val jingyingdizhi=binding.jingyingdizhi.text.toString()
            viewmodel.onbangdingyingyewang(UserData.token, mapOf("q_id_card" to sfzhao,"q_mobile" to shoujihao,"q_name" to qieyname,"q_email" to youxiangdizhi,"q_address" to jingyingdizhi,"yy_images" to tupiadizhi))

        }


    }

    override fun dataObservese() {

        viewmodel.onbangdingyingye.observe(this,Observer{
            if(it.code==1){
                finish()
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

            }


        })

        viewmodel.onlusc.observe(this, Observer {
            if(it.code==1){
                Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.url).into(binding.yingyezhizhaotu)
                Log.e("上传图片的半路径",it.data.url)

                tupiadizhi=it.data.url

            }
        })
    }


    private fun openGallery() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, GALLERY_REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            data?.data?.let { uri ->
                file = File(getRealPathFromURI(uri))

                val requestFile = file?.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                val body = MultipartBody.Part.createFormData("file", file?.name,requestFile!!)
                viewmodel.onluying(body)
            }
        }
    }

    private fun getRealPathFromURI(contentUri: Uri): String {
        val projection = arrayOf(MediaStore.Images.Media.DATA)
        val cursor = contentResolver.query(contentUri, projection, null, null, null)
        val column_index = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
        cursor?.moveToFirst()
        val result = cursor?.getString(column_index!!)
        cursor?.close()
        return result!!
    }
    companion object {
        private const val GALLERY_REQUEST_CODE = 1001
    }

}